<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-11-12 14:38:48
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Comprehensive;


use App\Http\Services\Service;
use App\Repositories\EmployeeRepository;
use App\Repositories\UserRepository;
use Illuminate\Support\Facades\Hash;

/**
 * @property UserRepository $user
 * @author sunkaiyuan
 */
class EmployeeService extends Service
    {
    public $details;
    public function __construct(EmployeeRepository $repository, UserRepository $user)
        {
        $this->repository = $repository;
        $this->user       = $user;
        }


    public function list()
        {
        $data = $this->repository->whereIs_fire(0)->where("id", "<>", "1")->condition(request())->getGrid();
        return $data;
        }
    public function resignationList()
        {
        $data = $this->repository->whereIs_fire(1)->condition(request())->getGrid();
        return $data;
        }
    public function details()
        {
        $data = $this->details->condition(request())->getList();
        return $data;
        }
    private function createUser($data)
        {
        $user_id = $data['user_id'] ?? 0;
        //用户名为空，取姓名
        $username = isEmpty($data['username'] ?? "") ? $data['name'] : $data["username"];
        $this->user->withTrashed()->whereUsername($username)->get()->filter(function ($item) use ($user_id)
            {
            if ($item->id != $user_id)
                fail("开启的账号登录名重复！请手动设置登录用户名！");
            else {
                if ($item->trashed()) {
                    $item->restore();
                    }
                }
            });

        //密码为空，取手机后六位
        $password = isEmpty($data['password'] ?? "") ? substr($data['phone'], -6) : $data["password"];
        $newdata  = [
            'id'            => $user_id,
            'name'          => $data['name'],
            'username'      => $username,
            'sex_id'        => $data['sex_id'],
            'phone'         => $data['phone'],
            'department_id' => $data['department_id'],
            'attachments'   => $data['attachments'],
            'password'      => Hash::make($password)
        ];

        return $this->user->autosave($newdata);
        }
    protected function save()
        {
        $data = request()->toArray();
        unset($data['avatar'], $data['thumb_avatar']);
        //开启账号
        $user_data = null;
        if ($data['en_account'] == 1) {
            $user_data       = $this->createUser($data);
            $data['user_id'] = $user_data->id;
            }
        else {
            $this->user->whereId($data['user_id'] ?? 0)->first()?->delete();
            }
        unset($data["password"]);
        $employee_data = $this->repository->autosave($data);

        if ($user_data) {
            $user_data->employee_id = $employee_data->id;
            $user_data->save();
            }
        }

    protected function delete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            $v->delete();
            });
        }
    protected function resignation()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            $v->user?->delete();
            $v->is_fire   = 1;
            $v->fire_date = date("Y-m-d H:i:s");
            ;
            return $v->save();
            });
        }
    protected function noresignation()
        {
        return $this->repository->withTrashed()->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            $user = $v->user()->withTrashed()->first();
            if ($user && $user->trashed())
                $user->restore();
            $v->is_fire = 0;
            return $v->save();
            });
        }
    }
